//
//  TTNotifyViewController.m
//  TaoTuo
//
//  Created by admin on 2019/10/22.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTNotifyViewController.h"
#import "TTNotifyViewCell.h"
#import "TTWebViewController.h"

static NSString *cellID = @"notify_cellID";

@interface TTNotifyViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NotifyPageType pageType;   // 公告=0,帮助=1

@end

@implementation TTNotifyViewController

- (instancetype)initWithPageType:(NotifyPageType)pageType
{
    self = [super init];
    if (self) {
        self.pageType = pageType;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.pageType == NotifyPageTypeWithNotify ? @"公告" : @"帮助";
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.page = 1;
        [self getNotifyData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.page++;
        [self getNotifyData];
    }];
    
    self.page = 1;
    [self getNotifyData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTNotifyViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TTNotifyModel *model = self.dataArray[indexPath.row];
    
    TTWebViewController *webVC = [[TTWebViewController alloc] init];
    webVC.notifyID = model.NoticeId;
    [self.navigationController pushViewController:webVC animated:YES];
    
}

- (void)getNotifyData
{
    NSDictionary *dic = @{
        @"param.pageIndex" : @(self.page),
        @"param.noticeType" : @(self.pageType),
        @"param.pageSize" : @40
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:notifyOrHelpList_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
        }
        NSArray *arr = responseObject;
        for (NSDictionary *dic in arr) {
            TTNotifyModel *model = [TTNotifyModel modelWithJSON:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
        if (arr.count < 40) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        @strongify(self)
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } showLoadingInView:self.view];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 50;
        _tableView.emptyImageName = @"nodata";
        _tableView.emptyTitel = @"暂无数据";
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        [_tableView registerClass:[TTNotifyViewCell class] forCellReuseIdentifier:cellID];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_topLayoutGuideTop);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _tableView;
}



- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



@end
